//
//  TopicPingViewController.m
//  news
//
//  Created by Qiaobuyong on 16/3/11.
//  Copyright © 2016年 imacbookair. All rights reserved.
//

#import "TopicPingViewController.h"
@interface TopicPingViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView        *tPTableView;
@property (nonatomic, strong) NSDictionary       *touDic;
@property (nonatomic, strong) NSArray            *remenArr;
@property (nonatomic, strong) NSArray            *zuijinArr;
@property (nonatomic, strong) UISegmentedControl *seg;
@end

@implementation TopicPingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.alpha = 1;
    self.navigationItem.title = self.titleStr;
    
    self.tabBarController.tabBar.hidden = YES;
    // tableView
    [self creatTableView];
    // 数据
    [self data];

}

#pragma mark - 数据请求
- (void)data
{
    NSString *userId = self.idStr;
    // id拼接
    NSString *urlStr = [NSString stringWithFormat:@"http://c.3g.163.com/newstopic/qa/%@.html", userId];
//    [NetworkHandler GETwithURL:urlStr completion:^(id responseObject) {
//        self.touDic = responseObject[@"data"][@"expert"];
//        self.remenArr = responseObject[@"data"][@"hotList"];
//        self.zuijinArr = responseObject[@"data"][@"latestList"];
//        [self hander];
//        
//        [self.tPTableView reloadData];
//    }];
    [NewWorking GetDayaWithURL:urlStr dic:nil sussess:^(id responseObject) {
        self.touDic = responseObject[@"data"][@"expert"];
        self.remenArr = responseObject[@"data"][@"hotList"];
        self.zuijinArr = responseObject[@"data"][@"latestList"];
        [self hander];
        
        [self.tPTableView reloadData];
    } filed:^(NSError *error) {
        
    }];
    
    
}

// 详情页上方的布局
- (void)hander
{
    UIView *hand = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300 * KHIGHT)];
    // 最上边的图
    UIImageView *tupian = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 180 * KHIGHT)];
    //    tupian.backgroundColor = [UIColor redColor];
    // 本人信息
    UIImageView *benren = [[UIImageView alloc] initWithFrame:CGRectMake(10 * KWITH, 185 * KHIGHT, self.view.frame.size.width - 20, 80 * KHIGHT)];
    //    benren.backgroundColor = [UIColor magentaColor];
    // 本人头像
    UIImageView *touxiang = [[UIImageView alloc] initWithFrame:CGRectMake(10 * KWITH, 10 * KHIGHT, 30 * KWITH, 30 * KHIGHT)];
    [touxiang sd_setImageWithURL:[NSURL URLWithString:_touDic[@"headpicurl"]]];
    // 本人的名字
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(50 * KWITH, 10 * KHIGHT, 100 * KWITH, 20 * KHIGHT)];
    name.font = [UIFont systemFontOfSize:12];
    name.text = [NSString stringWithFormat:@"%@|%@", _touDic[@"name"], _touDic[@"title"]];
    // 简介
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(50 * KWITH, 30 * KHIGHT, benren.frame.size.width - 60, benren.frame.size.height - 30)];
    title.text = _touDic[@"description"];
    title.font = [UIFont systemFontOfSize:14];
    title.numberOfLines = 0;
    // 提问人数
    UILabel *tiWen = [[UILabel alloc] initWithFrame:CGRectMake(15 * KWITH, 270 * KHIGHT, 200 * KWITH, 25 * KHIGHT)];
    //    tiWen.backgroundColor = [UIColor purpleColor];
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"最新", @"最热"]];
    _seg.frame = CGRectMake(260 * KWITH, 270 * KHIGHT, 80 * KWITH, 25 * KHIGHT);
    _seg.selectedSegmentIndex = 0;
    [self.seg addTarget:self action:@selector(seg:) forControlEvents:UIControlEventValueChanged];
    
    [tupian sd_setImageWithURL:[NSURL URLWithString:_touDic[@"picurl"]]];
    [benren addSubview:touxiang];
    [benren addSubview:name];
    [benren addSubview:title];
    [hand addSubview:_seg];
    [hand addSubview:tiWen];
    [hand addSubview:benren];
    [hand addSubview:tupian];
    [self.tPTableView.tableHeaderView addSubview:hand];
}

- (void)seg:(UISegmentedControl *)seg
{
    [self.tPTableView reloadData];
}

#pragma mark - 创建tableView
- (void)creatTableView
{
    // 创建tableView
    self.tPTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    // 签订协议
    _tPTableView.delegate = self;
    _tPTableView.dataSource = self;
    // tableView左侧滑动条与cell分割线隐藏
    _tPTableView.showsVerticalScrollIndicator = NO;
    _tPTableView.separatorStyle = NO;
    _tPTableView.contentInset = UIEdgeInsetsMake(-20, 0, 59 * KWITH, 0);
    // 创建tableView的头部 用于加轮播图
    UIView *hander = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 300 * KHIGHT)];
    //    hander.backgroundColor = [UIColor greenColor];
    _tPTableView.tableHeaderView = hander;
    [self.view addSubview:_tPTableView];
    [_tPTableView registerClass:[TopicCommentTableViewCell class] forCellReuseIdentifier:@"cell"];

}

#pragma mark - tableView的协议方法实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_seg.selectedSegmentIndex == 0) {
        return _zuijinArr.count;
    } else {
        return _remenArr.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_seg.selectedSegmentIndex == 0) {
        NSDictionary *zuiJinDic = _zuijinArr[indexPath.row];
        TopicCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        [cell.userHeadImage sd_setImageWithURL:[NSURL URLWithString:zuiJinDic[@"question"][@"userHeadPicUrl"]]];
        cell.userName.text = zuiJinDic[@"question"][@"userName"];
        cell.question.text = zuiJinDic[@"question"][@"content"];
        [cell.headImage sd_setImageWithURL:[NSURL URLWithString:zuiJinDic[@"answer"][@"specialistHeadPicUrl"]]];
        cell.name.text = zuiJinDic[@"answer"][@"specialistName"];
        cell.answer.text = zuiJinDic[@"answer"][@"content"];
        NSNumber *time = zuiJinDic[@"question"][@"ccTime"];
        cell.time.text = [TimeChange compareCurrentTime:time];
        return cell;
    } else {
        NSDictionary *remenDic = _remenArr[indexPath.row];
        TopicCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        [cell.userHeadImage sd_setImageWithURL:[NSURL URLWithString:remenDic[@"question"][@"userHeadPicUrl"]]];
        cell.userName.text = remenDic[@"question"][@"userName"];
        cell.question.text = remenDic[@"question"][@"content"];
        [cell.headImage sd_setImageWithURL:[NSURL URLWithString:remenDic[@"answer"][@"specialistHeadPicUrl"]]];
        cell.name.text = remenDic[@"answer"][@"specialistName"];
        cell.answer.text = remenDic[@"answer"][@"content"];
        NSNumber *time = remenDic[@"question"][@"ccTime"];
        cell.time.text = [TimeChange compareCurrentTime:time];
        return cell;
    }
}

// cell高度设置
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_seg.selectedSegmentIndex == 0) {
        NSDictionary *zuiJinDic = _zuijinArr[indexPath.row];
        CGFloat h = [TopicCommentTableViewCell heightWihtText:zuiJinDic[@"question"][@"content"]];
        CGFloat r = [TopicCommentTableViewCell heightWihtText:zuiJinDic[@"answer"][@"content"]];
        return h + r + 150 * KHIGHT;
    } else {
        NSDictionary *remenDic = _remenArr[indexPath.row];
        CGFloat h = [TopicCommentTableViewCell heightWihtText:remenDic[@"question"][@"content"]];
        CGFloat r = [TopicCommentTableViewCell heightWihtText:remenDic[@"answer"][@"content"]];
        return h + r + 150 * KHIGHT;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y >= 240) {
        self.navigationController.navigationBar.alpha = 1;
    } else {
        self.navigationController.navigationBar.alpha = 0;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
